/**
 * 工具类
 */

 /**
  * 格式化时间
  * @param {*} date 
  */
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
/**
 * 毫秒转换成时分秒
 */
const formatMillisecondTime = endtime => {
  let newTime = new Date().getTime();
  let endTime = endtime;
  let remainTime = endTime - newTime;
  // 时分秒
  let end = '00:00:00'; 
  // 如果差额大于0，对时间进行处理
  if (remainTime > 0) {
    let time = remainTime / 1000;
    // 获取天、时、分、秒
    //let day = parseInt(time / (60 * 60 * 24));
    let hou = parseInt(time % (60 * 60 * 24) / 3600);
    let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
    let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
    obj = {
      //day: formatNumber(day),
      hou: formatNumber(hou),
      min: formatNumber(min),
      sec: formatNumber(sec)
    }
    end = hou + ':' + min + ':' + sec;
  }
  return end;
}

/**
 * 判断对象是否为空
 * @param {object} str 
 */
const isBlank = str =>{
  if (Object.prototype.toString.call(str) ==='[object Undefined]'){//空
    return true
  } else if (
    Object.prototype.toString.call(str) === '[object String]' || 
    Object.prototype.toString.call(str) === '[object Array]') { //字条串或数组
    return str.length==0?true:false
  } else if (Object.prototype.toString.call(str) === '[object Object]') {
    return JSON.stringify(str)=='{}'?true:false
  }else{
    return true
  }

}

module.exports = {
  formatTime: formatTime,
  isBlank: isBlank
}
